package com.szy.douyou_gateway.web;

import com.szy.douyou_gateway.config.AccountProfile;
import com.szy.douyou_gateway.config.ResultDTO;
import com.szy.douyou_gateway.dao.UserReq;
import com.szy.douyou_gateway.dao.Users;
import com.szy.douyou_gateway.service.UsersService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户controller
 * @author sunziyan
 * @date 2022/4/4
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    private UsersService usersService;

    /**
     * 编辑用户个人信息
     * @param user
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/updateMyMessage")
    public ResultDTO<Users> UpdateMyMessage(@RequestBody Users user) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if(!user.getUserId().equals(principal.getUserId())){
            return ResultDTO.fail("不能编辑别人的个人信息");
        }

        return usersService.updateOneUser(user);
    }

    /**
     * 后台页面分页查询用户
     * @param userReq
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/queryPage")
    public ResultDTO queryPage(@RequestBody UserReq userReq) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();

        if(!principal.getUserRight().equals(1L)){
            return ResultDTO.fail("没有管理员权限");
        }
        return usersService.queryUser(userReq);
    }

    /**
     * 删除用户账号
     * @param userId
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/deleteUser/{userId}")
    public ResultDTO deleteUser(@PathVariable("userId") Long userId) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();

        if(principal.getUserRight() == 1){
            return usersService.deleteUser(userId);
        }else {
            return ResultDTO.fail("没有管理员权限");
        }
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/getOneUser/{userId}")
    public ResultDTO getOneUser(@PathVariable("userId")Long userId){
        return ResultDTO.success(usersService.getUserById(userId));
    }

    /**
     * 修改密码
     * @param userId
     * @param userPassword
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/changePassword/{userId}/{userPassword}")
    public ResultDTO changePassword(@PathVariable("userId")Long userId,@PathVariable("userPassword") String userPassword){
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if(!principal.getUserId().equals(userId)){
            return ResultDTO.fail("无权限修改密码");
        }
        return usersService.changePassword(userId,userPassword);
    }




}
